package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.BussGoodsTypeDao;
import com.baichang.taoshunda.dao.BussTypeDao;
import com.baichang.taoshunda.dao.GoodsTypeDao;
import com.baichang.taoshunda.entity.BussType;
import com.baichang.taoshunda.entity.GoodsType;
import com.baichang.taoshunda.service.BusinessService;
import com.baichang.taoshunda.service.BussTypeService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 2017/8/18 0018.
 */

@Service
public class BussTypeServiceImpl implements BussTypeService {
    @Autowired
    private BussTypeDao bussTypeDao;
    @Autowired
    private GoodsTypeDao goodsTypeDao;
    @Autowired
    private BusinessService businessService;

    /**
     * 首页获取商家分类
     *
     * @param cityId 城市编号
     * @return
     */
    @Override
    public List<Map<String, Object>> getHomePageType(Integer cityId) {
        return bussTypeDao.getHomePageType(cityId);
    }

    /**
     * 查询首页分类
     *
     * @param name
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public Page queryHomePage(String name, int nowPage, int pageSize) {
        return bussTypeDao.queryHomePag(name, nowPage, pageSize);
    }


    /**
     * 添加首页分类
     *
     * @param cityId
     * @param typeName
     * @param typePic
     * @param price
     * @param money
     * @param proportionate
     * @return
     */
    @Override
    public long addHomePageType(Integer cityId, Integer rank, String typeName, String typePic, BigDecimal price, BigDecimal money, String proportionate) throws ServiceException {
        if (cityId == -1) {
            throw new ServiceException("请选择城市");
        }
        List<Map<String, Object>> list = bussTypeDao.getTypeName();
        for (int i = 0; i < list.size(); i++) {
            String name = list.get(i).get("name").toString();
            if (name.equals(typeName)) {
                throw new ServiceException("请不要重复添加分类名称");
            }
        }
        BussType bussType = new BussType();
        bussType.setTypeName(typeName);
        bussType.setRank(rank);
        bussType.setTypePic(typePic);
        bussType.setCreated(new Date());
        bussType.setState(1);
//        bussType.setCityId(cityId);
        bussType.setPrice(price);
        bussType.setMoney(money);
        bussType.setProportionate(proportionate);
        return bussTypeDao.save(bussType);
    }

    /**
     * 删除
     *
     * @param id
     * @param state
     * @return
     */
    @Override
    public int updateState(Integer id, Integer state) throws ServiceException {
        int companyCount = businessService.getCompanyCountByTypeId(id);
        if (companyCount > 0) {
            throw new ServiceException("该分类下存在商家，不支持删除");
        }
        return bussTypeDao.updateState(id, state);
    }

    /**
     * 修改首页分类
     *
     * @param id
     * @param rank
     * @param typeName
     * @param typePic
     * @param price
     * @param money
     * @param proportionate
     * @return
     */
    @Override
    public int updateHomePageType(Integer id, Integer rank, Integer cityId, String typeName, String typePic, BigDecimal price, BigDecimal money, String proportionate) {
        if (!typeName.equals("")) {
            Map map = new HashedMap();
            map.put("typeName", typeName);
            map.put("typePic", typePic);
//            map.put("cityId", cityId);
            map.put("price", price);
            map.put("money", money);
            map.put("proportionate", proportionate);
            return bussTypeDao.update(map, id);
        } else {
            return bussTypeDao.updateRank(id, rank);
        }
    }

    /**
     * 获取分类信息
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> queryByTypeId() {
        return bussTypeDao.queryByTypeId();
    }

    /**
     * 查询商品分类
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> queryGoodsType() {
        return goodsTypeDao.queryStateGoodsType();
    }

    @Override
    public List<Map<String, Object>> getCompanyType(Integer cityId) {
        return bussTypeDao.getCompanyType();
    }
}
